package com.example.countscore;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class config extends AppCompatActivity {

    public static final String TAG = "currencyExchange";

    float dollar2, euro2, won2;
    float dollar3, euro3, won3;
    EditText dollar_rate, euro_rate, won_rate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config);

        //使用intent接收当前汇率值
        Intent intent = getIntent();
        dollar2 = intent.getFloatExtra("dollar_rate_key", 0.0f);
        euro2 = intent.getFloatExtra("euro_rate_key", 0.0f);
        won2 = intent.getFloatExtra("won_rate_key", 0.0f);

        Log.i(TAG, "onCreate: dollar2=" + dollar2);
        Log.i(TAG, "onCreate: euro2=" + euro2);
        Log.i(TAG, "onCreate: won2=" + won2);

        //显示当前汇率值
        dollar_rate = findViewById(R.id.dollar_rate);
        dollar_rate.setText(String.valueOf(dollar2));
        euro_rate = findViewById(R.id.euro_rate);
        euro_rate.setText(String.valueOf(euro2));
        won_rate = findViewById(R.id.won_rate);
        won_rate.setText(String.valueOf(won2));

    }

    //点击“Save”按钮
    public void clickSave(View v){

        //获取当前输入值，并存入
        dollar3 = Float.valueOf(dollar_rate.getText().toString());
        euro3 = Float.valueOf(euro_rate.getText().toString());
        won3 = Float.valueOf(won_rate.getText().toString());

//        //存入两页面传值变量，跳转回原页面
//        Intent save = new Intent(this, currencyExchange.class);

//        //使用intent存入页面传递变量
        Intent save = getIntent();
        save.putExtra("dollar_rate_key2", dollar3);
        save.putExtra("euro_rate_key2", euro3);
        save.putExtra("won_rate_key2", won3);

        //使用sharePreference修改保存内容
        SharedPreferences sp = getSharedPreferences("myrate", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putFloat("dollar_rate_key2", dollar3);
        editor.putFloat("euro_rate_key2", euro3);
        editor.putFloat("won_rate_key2", won3);
        editor.apply();


        Log.i(TAG, "clickSave: dollarRate=" + dollar3);
        Log.i(TAG, "clickSave: euroRate=" + euro3);
        Log.i(TAG, "clickSave: wonRate=" + won3);

        setResult(3,save);

//        startActivity(currencyExchange);
        //关闭当前窗口
        finish();
    }
}